const glMatrix = require('gl-matrix');
const vec3 = glMatrix.vec3;

module.exports = class Line {
    constructor(position, direction) {
        this.vertice = position.slice(0);
        this.vector = vec3.create();
        vec3.normalize(this.vector, direction);
    }

    setVertice(position) {
        this.vertice = position.slice(0);
        return this;
    }

    setVector(direction) {
        this.vector = vec3.create();
        vec3.normalize(this.vector, direction);
        return this;
    }

    clone() {
        var lineCopy = new Line(this.vertice, this.vector);
        return lineCopy;
    }

    // 已知z求xy
    getXYByZ(z) {
        const m = (z - this.vertice[2]) / this.vector[2];
        const x = m * this.vector[0] + this.vertice[0];
        const y = m * this.vector[1] + this.vertice[1];
        return [x, y];
    }
};